package foodtable;

import java.util.List;

import com.andro.chansung.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FoodAdapter extends BaseAdapter {

	private List<FoodList> foodLists;
	private LayoutInflater inflater;
	private Context context;
	private ViewHolder viewHolder;
	
	private class ViewHolder {
		public TextView timeTv  = null;
		public TextView kindATv = null;
		public TextView kindBTv = null;
		public TextView kindCTv = null;
	}
	
	public FoodAdapter(Context context, List<FoodList> foodList) {
		this.context = context;
		this.foodLists = foodList;
		this.inflater = LayoutInflater.from(context);
	}
	
	public int getCount() {
		return foodLists.size();
	}

	public FoodList getItem(int position) {
		return foodLists.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		
		if( view == null ) {
			viewHolder = new ViewHolder();
			view = inflater.inflate(R.layout.food_table_list_view_cell_layout, null);
			viewHolder.timeTv  = (TextView)view.findViewById(R.id.food_list_view_cell_time_tv);
			viewHolder.kindATv = (TextView)view.findViewById(R.id.food_menu_kind_a_tv);
			viewHolder.kindBTv = (TextView)view.findViewById(R.id.food_menu_kind_b_tv);
			viewHolder.kindCTv = (TextView)view.findViewById(R.id.food_menu_kind_c_tv);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder)view.getTag();
		}
		
		FoodList foodList = getItem(position);
		List<Food> foods = foodList.getFoods();
		viewHolder.timeTv.setText(foods.get(0).getTime().toString());
		viewHolder.kindATv.setText(foods.get(0).getMenu());
		viewHolder.kindBTv.setText(foods.get(1).getMenu());
		
		LinearLayout layout = (LinearLayout)view.findViewById(R.id.food_table_kind_c_layout);
		
		if( foods.size() > 2 ) {
			layout.setVisibility(View.VISIBLE);
			viewHolder.kindCTv.setText(foods.get(2).getMenu());
		} else {
			layout.setVisibility(View.GONE);
		}
		return view;
	}
}
